# -*- coding: utf-8 -*-
from gevent import monkey
#听说是个补丁
monkey.patch_all()
'''
urls = ["4d00b50cebd6400d"]
def fetch_url(url):
    d = Device('%s'%(url))
    print d.info
    d.click(100,100)
def adb():
    print ''.join(os.popen('adb devices').readlines())

gevent.joinall([
    gevent.spawn(fetch_url,urls[0]),
    gevent.spawn(adb),
])


#队列
import gevent
import time
start = time.clock()
from gevent.queue import Queue
tasks = Queue()
def worker(n):
    #empty()队列为空返回True:
    while not tasks.empty():
        task = tasks.get()
        print('Worker %s got task %s' % (n, task))
        
    print('Quitting time!')
        
def boss():
    a='asdf'
    tasks.put_nowait(a)

boss()

gevent.joinall([gevent.spawn(worker, 'steve'),
                gevent.spawn(worker, 'hello1'),
                gevent.spawn(worker, 'hello2')
                ])

end = time.clock()
print "read: %f s" % (end - start)


import gevent

def f(n):
    for i in range(n):
        print gevent.getcurrent(), i

g1 = gevent.spawn(f, 5)
g2 = gevent.spawn(f, 5)
g3 = gevent.spawn(f, 5)
g1.join()
g2.join()
g3.join()
'''

import gevent
import urllib2
import time
from gevent import Greenlet  



def f():
    print('GET: %s' % url)
    resp = urllib2.urlopen(url)
    data = resp.read()
    #print type(data)
    print('%d bytes received from %s.' % (len(data), url))
    txt = open(r'c:\%s.txt'%(name),'w+')
    T = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
    data = data + str(T)
    txt.write(data)
    txt.close
    gevent.sleep(0)



    
def j(neam):
    print neam

def  test1(a):
	print 1,a
	#交出控制权，让test2执行
	#等test2执行完毕后再执行后面的
	#print 'okok',a
	gevent.sleep(0)
	print 'okok',a
def test2(a):
	print 2,a
	gevent.sleep(0)
	print 'nono',a


gevent.joinall([
        gevent.spawn(f, 'http://www.163.com','a'),
        #gevent.spawn(j, 'zhonghuanm'),
        gevent.spawn(f, 'http://www.qq.com','b'),
        #gevent.spawn(j, 'zhonghuanm'),
])
